<?php

class Work_LoginController extends My_Work_Controller {

    public static $_uriLogin;
    public static $_uriLoginCheck;
    private $_form;

    public function initAdmin() {
        self::$_uriLogin = ZFKiller_View_Helper_Url::urlStatic(array('action' => 'index'), null, true, false);
        self::$_uriLoginCheck = ZFKiller_View_Helper_Url::urlStatic(array('action' => 'check'), null, true, false);
    }

    public function preDispatch() {
        if (Zend_Auth::getInstance()->hasIdentity()) {
            // If the user is logged in, we don't want to show the login form;
            // however, the logout action should still be available
            if ('logout' != $this->getRequest()->getActionName()) {
                $this->_helper->redirector('index', 'index');
            }
        }
        /* else {
          // If they aren't, they can't logout, so that action should
          // redirect to the login form
          if ('logout' == $this->getRequest()->getActionName()) {
          $this->_helper->redirector('index');
          }
          } */
    }

    public function checkAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        if ($this->getRequest()->isPost()) {
			$this->initFormLogin();
            if ($this->_form->isValid($this->getRequest()->getPost())) {
                $objAuth = Zend_Auth::getInstance();

                $authAdapter = new Zend_Auth_Adapter_DbTable(
                                Zend_Db_Table::getDefaultAdapter(),
                                'TbAdmin',
                                'stemail',
                                'stsenha'
                );

                // http://www.danieldeveloper.com/2010/02/06/autenticacao-com-zend_auth/
                // http://weierophinney.net/matthew/archives/165-Login-and-Authentication-with-Zend-Framework.html
                $authAdapter->setIdentity($this->_form->getValue('stemail'))->setCredential($this->_form->getValue('stsenha'));
                $result = $objAuth->authenticate($authAdapter);
                if ($result->isValid()) {
                    $authData = $authAdapter->getResultRowObject(null, 'stsenha');
                    $objAuth->getStorage()->write($authData);

					$this->_helper->redirector->gotoSimpleAndExit('index', 'index');
                } else {
                    ZFKiller_Msg::addMsg('Email e/ou Senha incorretos.', ZFKiller_Msg::MSG_ERROR);
                }
            } else {
                foreach ($this->_form->getMessages() as $elementName => $messagesElement) {
                    $element = $this->_form->getElement($elementName);
                    foreach ($messagesElement as $message) {
                        ZFKiller_Msg::addMsg($element->getLabel() . ' ' . $message, ZFKiller_Msg::MSG_WARN);
                    }
                }
            }
        }
        $this->_forward('index');
    }

    public function indexAction() {
        $this->_helper->layout->setLayout('login');
		$this->_helper->viewRenderer->setNoRender(true);

		$this->initFormLogin();
        $this->view->form = $this->_form;
    }

    public function logoutAction() {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect(self::$_uriLogin);
    }

    private function initFormLogin() {
        if (is_null($this->_form)) {
            $this->_form = new Work_Form_Login();
        }
    }

}